home *** CD-ROM | disk | FTP | other *** search
/ Nautilus 1993 March / Nautilus-4-3 / Nautilus-4-3.bin / Multimedia / Feature / RlePict1.1 Folder / macstuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-08  |  10.6 KB  |  483 lines

  1. /*
  2.  * This software is copyrighted as noted below.  It may be freely copied,
  3.  * modified, and redistributed, provided that the copyright notice is 
  4.  * preserved on all copies.
  5.  * 
  6.  * There is no warranty or other guarantee of fitness for this software,
  7.  * it is provided solely "as is".  Bug reports or fixes may be sent
  8.  * to the author, who may or may not act on them as he desires.
  9.  *
  10.  * You may not include this software in a program or other software product
  11.  * without supplying the source, or without informing the end-user that the 
  12.  * source is available for no extra charge.
  13.  *
  14.  * If you modify this software, you should include a notice giving the
  15.  * name of the person performing the modification, the date of modification,
  16.  * and the reason for such modification.
  17.  */
  18. /*
  19.  * Generic mac UI doodads for the RLE converter
  20.  *
  21.  * John Peterson - Jan/Feb '91
  22.  *
  23.  * Copyright (c) 1991 John Peterson
  24.  *
  25.  */
  26.  
  27.  
  28. #include <QDOffscreen.h>
  29.  
  30. #include "macstuff.h"
  31. #include <Stdio.h>
  32. #include <string.h>
  33. #include <pascal.h>
  34.  
  35. static MenuHandle appleMenu, fileMenu, editMenu;
  36.  
  37.  
  38. /*******************************************************************
  39.  *                         F I L E N A M E   S T U F F                *
  40.  *******************************************************************/
  41.  
  42. /*
  43.  * Prompt for an existing filename using the StdFile dialogs
  44.  * (i.e., for an "Open..." menu item).
  45.  */
  46.  
  47. char *
  48. OldFilename( prompt, filename, type )
  49. char * prompt;
  50. char * filename;
  51. OSType type;
  52. {
  53.     static Point dlogWhere = { 70, 40 };
  54.     short numTypes = (type == '????') ? -1 : 1;
  55.     SFReply imgFile;
  56.     char pprompt[200];
  57.     
  58.     strcpy( pprompt, prompt );
  59.     CtoPstr( pprompt );
  60.     SFGetFile( dlogWhere, pprompt, NULL, numTypes, &type, NULL, &imgFile );
  61.     if (! imgFile.good)
  62.     {
  63.         filename = NULL;
  64.         return( NULL );
  65.     }
  66.  
  67.     SetVol( NULL, imgFile.vRefNum ); /* Go to the directory */
  68.     PtoCstr( (char *) imgFile.fName );
  69.     strcpy( filename, (char *)imgFile.fName );
  70.     return( filename );
  71. }
  72.  
  73. /*
  74.  * Prompt for a new filename using the StdFile dialogs
  75.  * (i.e., for an "Save As..." menu item).
  76.  */
  77.  
  78. char *
  79. NewFilename( prompt, filename )
  80. char * prompt;
  81. char * filename;
  82. {
  83.     static Point dlogWhere = { 70, 40 };
  84.     SFReply imgFile;
  85.     char pprompt[200];
  86.     char pfname[200];
  87.     
  88.     strcpy( pfname, filename );
  89.     CtoPstr( pfname );
  90.     strcpy( pprompt, prompt );
  91.     CtoPstr( pprompt );
  92.     SFPutFile( dlogWhere, pprompt, pfname, NULL, &imgFile );
  93.     if (! imgFile.good)
  94.     {
  95.         filename = NULL;
  96.         return( NULL );
  97.     }
  98.  
  99.     SetVol( NULL, imgFile.vRefNum ); /* Go to the directory */
  100.     PtoCstr( (char *) imgFile.fName );
  101.     strcpy( filename, (char *)imgFile.fName );
  102.     return( filename );
  103. }
  104.  
  105. /*******************************************************************
  106.  *                         E R R O R   A L E R T S                       *
  107.  *******************************************************************/
  108.  
  109. /*
  110.  * Handle generic error messages.
  111.  */
  112. void 
  113. ErrorAlert( char * s )
  114. {
  115.     char msg[255];
  116.     char * p;
  117.     
  118.     strcpy( msg, s );
  119.     /* Kill newlines */
  120.     for (p = msg; *p; p++)
  121.         if (*p == '\n') *p = ' ';
  122.     CtoPstr( msg );
  123.     ParamText( msg, NIL, NIL, NIL );
  124.     Alert( 200, NIL );
  125. }
  126.  
  127. /*
  128.  * Memory errors are always fatal.  We can just happily exit - because
  129.  * this program does no editing, no user data will be lost.
  130.  */
  131. void
  132. MemoryError( char * s )
  133. {
  134.     DisposPtr( PanicStash );
  135.     ErrorAlert( s );
  136.     ExitToShell();
  137. }
  138.  
  139. void
  140. ToolboxError(char * msg, int num)
  141. {
  142.     char alertmsg[255];
  143.     
  144.     if (num)
  145.     {
  146.         sprintf( alertmsg, "%s (code=%d)", msg, num );
  147.         ErrorAlert( alertmsg );
  148.     }
  149. }
  150.  
  151. /*******************************************************************
  152.  *                         W I N D O W   C R E A T I O N               *
  153.  *******************************************************************/
  154.  
  155. /*
  156.  * Find the "best" color screen (with the most colors)
  157.  */
  158. Boolean ColorScreen(best)
  159. Rect * best;
  160. {
  161.     Rect r;
  162.     GDHandle QDDev;
  163.     Boolean mainScr;
  164.     int bestScreen = 0;
  165.         
  166.     QDDev = GetDeviceList();
  167.     mainScr = (*QDDev)->gdFlags & (1<<mainScreen); 
  168.     while (QDDev)
  169.     {
  170.         if ((*((*QDDev)->gdPMap))->pixelSize >= bestScreen)
  171.         {
  172.             bestScreen = (*((*QDDev)->gdPMap))->pixelSize;
  173.             *best = (*QDDev)->gdRect;
  174.             mainScr = (*QDDev)->gdFlags & (1<<mainScreen); 
  175.         }
  176.         QDDev = GetNextDevice( QDDev );
  177.     }
  178.     return( mainScr );
  179. }
  180.  
  181. /*
  182.  * Create a window on the most colorful screen.
  183.  */
  184. WindowPtr 
  185. MakeWindow(width, height, title)
  186. int width, height;
  187. char * title;
  188. {
  189.     Rect r, best;
  190.     static int vOffset = 40, hOffset = 20, rowcount = 1;
  191.     WindowPtr win;
  192.     
  193.     CtoPstr( title );
  194.     (void) ColorScreen( &best ) ;
  195.     SetRect( &r, hOffset, vOffset, hOffset+width, vOffset+height );
  196.     OffsetRect( &r, best.left, best.top );
  197.     win = (WindowPtr) NewCWindow( NULL, &r, title,
  198.                                           true, noGrowDocProc,
  199.                                           (Ptr) -1L, true, 0L );
  200.     PtoCstr( title );    /* Munge it back... */
  201.     
  202.     /* Stagger the windows as they're opened */
  203.     
  204.     hOffset += 20;
  205.     vOffset += 40;
  206.     if (vOffset > 300)
  207.     {
  208.         rowcount++;
  209.         vOffset = 40 + rowcount * 20;
  210.         hOffset = rowcount * 20;
  211.         if (hOffset > 500) hOffset = 20;
  212.     }      
  213.     
  214.     if (!win)
  215.         MemoryError( "Couldn't create window - out of memory" );
  216.     return win;
  217. }
  218.  
  219. /*******************************************************************
  220.  *                 P R O G R E S S   I N D I C A T O R                   *
  221.  *******************************************************************/
  222.  
  223.  
  224. DialogPtr InitProgress( short dlogID )
  225. {
  226.     DialogPtr progressDlog;
  227.     progressDlog = GetNewDialog( dlogID, NULL, (Ptr) -1L );
  228.     if (! progressDlog)
  229.         MemoryError( "Couldn't load progress indicator" );
  230.     DrawDialog( progressDlog );
  231.     SetPort( progressDlog );
  232.     return( progressDlog );
  233. }
  234.  
  235. void UpdateProgress( DialogPtr dlog, double fraction )
  236. {
  237.     short type;
  238.     Handle item;
  239.     Rect r;
  240.     
  241.     GetDItem( dlog, 2, &type, &item, &r );
  242.     FrameRect( &r );
  243.     InsetRect( &r, 2, 2 );
  244.     r.right = r.left + (r.right - r.left) * fraction;
  245.     FillRect( &r, gray );
  246. }
  247.  
  248. void DoneProgress( DialogPtr dlog )
  249. {
  250.     DisposDialog( dlog );
  251. }
  252.  
  253. /*******************************************************************
  254.  *                                 M E N U S                           *
  255.  *******************************************************************/
  256.  
  257. void 
  258. CreateMenus()
  259. {
  260.     appleMenu = GetMenu( 100 );                /* Apple Menu */
  261.     if (! appleMenu)
  262.         MemoryError( "Missing Apple menu?" );
  263.     AddResMenu( appleMenu, 'DRVR' );        /* Add all the desk accys */
  264.     InsertMenu( appleMenu, 0 );
  265.     
  266.     fileMenu = GetMenu( 200 );
  267.     if (! fileMenu)
  268.         MemoryError( "Missing file menu?" );        
  269.     InsertMenu( fileMenu, 0 );
  270.  
  271.     editMenu = GetMenu( 300 );
  272.     if (! editMenu)
  273.         MemoryError( "Missing edit menu?" );        
  274.     InsertMenu( editMenu, 0 );
  275.     DrawMenuBar();
  276. }
  277.  
  278. static 
  279. DoAppleItem (item)
  280.     int    item;
  281. {
  282.     GrafPtr    curPort;
  283.     Str255    str;
  284.     Handle    h;
  285.  
  286.     if (item == 1)
  287.     {
  288.         Alert( 400, NIL );        /* Bring up the about box */
  289.     }
  290.     else
  291.     {
  292.         GetPort (&curPort);
  293.         GetItem (appleMenu, item, str);        /* get DA name */
  294.         SetResLoad (false);
  295.         h = GetNamedResource ('DRVR', str);
  296.         SetResLoad (true);
  297.         if (h != NULL)
  298.         {
  299.             ResrvMem (SizeResource (h) + 0x1000);
  300.             (void) OpenDeskAcc (str);            /* open it */
  301.         }
  302.         SetPort (curPort);
  303.     }
  304. }
  305.  
  306. /*******************************************************************
  307.  *                             E V E N T   L O O P                       *
  308.  *******************************************************************/
  309.  
  310. void
  311. EventLoop()
  312. {
  313.     EventRecord event;
  314.     long menuCode, part;
  315.     WindowPtr hitWin, updateWin;
  316.     ImageWin * imgWin;
  317.     Rect r;
  318.     static Boolean editMenuOn = false;
  319.     
  320.     do
  321.     {
  322.         menuCode = 0;
  323.         
  324.         if (GetNextEvent( mDownMask+mUpMask+keyDownMask+updateMask+activMask, &event))
  325.         {
  326.             if ((event.what == keyDown) && (event.modifiers & cmdKey))
  327.             {
  328.                 menuCode = MenuKey( event.message & charCodeMask );
  329.             }
  330.                 
  331.             if ( (event.what == mouseDown) && (event.where.v < MBarHeight))
  332.                 menuCode = MenuSelect( event.where );
  333.  
  334.             if ( (event.what == mouseDown ))
  335.             {
  336.                 part = FindWindow( event.where, &hitWin );
  337.                 switch (part)
  338.                 {
  339.                 case inSysWindow:
  340.                     SystemClick( &event, hitWin );
  341.                     break;
  342.                     
  343.                 case inGrow:
  344.                     /* Grow stuff goez here */
  345.                     break;
  346.                     
  347.                 case inGoAway:
  348.                     if (TrackGoAway( hitWin, event.where ))
  349.                     {
  350.                         imgWin = (ImageWin *) GetWRefCon( hitWin );
  351.                         if (imgWin->tag != 0xDEADBEEF)
  352.                         {
  353.                             ErrorAlert("Not my window!");
  354.                             break;
  355.                         }
  356.                         SetPort( thePort );        /* Avoid dangling port */
  357.                         DisposeWindow( hitWin );
  358.                         DisposeGWorld( imgWin->gw );
  359.                     }
  360.                     break;
  361.  
  362.                     
  363.                 case inDrag:
  364.                     r = (*GrayRgn)->rgnBBox;
  365.                     InsetRect( &r, 4, 4 );        /* Keep it on screen */
  366.                     DragWindow( hitWin, event.where, &r );
  367.                     break;
  368.                     
  369.                 case inContent:
  370.                     SelectWindow( hitWin );
  371.                     /* maybe report RGB, or somesuch? */
  372.                     break;
  373.                     
  374.                 }
  375.             }
  376.             
  377.             if (event.what == updateEvt)
  378.             {
  379.                 /* window to be updated is in theEvent->message */
  380.                 updateWin = (WindowPtr) event.message;
  381.                 imgWin = (ImageWin *) GetWRefCon( updateWin );
  382.                 if (imgWin->tag != 0xDEADBEEF)
  383.                     ErrorAlert("Unidentified flying window...");
  384.                     
  385.                 BeginUpdate( updateWin );
  386.                 CopyGWtoWindow( imgWin->gw, updateWin );                
  387.                 EndUpdate( updateWin );
  388.             }
  389.             
  390.             if (event.what == activateEvt)
  391.             {
  392.                 updateWin = (WindowPtr) event.message;
  393.                 if ( (((WindowPeek)updateWin)->windowKind == userKind)
  394.                     && (event.modifiers & activeFlag))
  395.                 {
  396.                     SetPort( updateWin );
  397.                     
  398.                     if (editMenuOn)
  399.                     {
  400.                         EnableItem( fileMenu, 0 );
  401.                         DisableItem( editMenu, 0 );
  402.                         DrawMenuBar();
  403.                         editMenuOn = false;
  404.                     }
  405.                 }
  406.                 if ( (! editMenuOn) && (((WindowPeek)FrontWindow())->windowKind != userKind))
  407.                 {
  408.                     EnableItem( editMenu, 0 );
  409.                     DisableItem( fileMenu, 0 );
  410.                     DrawMenuBar();
  411.                     editMenuOn = true;
  412.                 }
  413.             }
  414.         }
  415.         
  416.         if (menuCode >> 16L)
  417.         {
  418.             switch ( menuCode >> 16L )
  419.             {
  420.             case 100: 
  421.                 DoAppleItem( (short) (menuCode & 0xFFFF) );
  422.                 break;
  423.                 
  424.             case 200:
  425.                 DoFileItem( (short) (menuCode & 0xFFFF) );
  426.                 break;
  427.  
  428.             default:
  429.                 ErrorAlert("Menu from outer space..." );
  430.                 break;
  431.             }
  432.         }
  433.  
  434.         HiliteMenu( 0 );
  435.         SystemTask();
  436.     } while(1);
  437. }
  438.  
  439. /*******************************************************************
  440.  *                         I N I T I A L I Z A T I O N                   *
  441.  *******************************************************************/
  442.  
  443. /*
  444.  * Check to make sure this Mac can run the program.
  445.  */
  446. static void 
  447. CheckGoodMac()
  448. {
  449.     SysEnvRec env;
  450.     
  451.     SysEnvirons( 1, &env );
  452.     if (env.systemVersion < 0x0605)
  453.         MemoryError("Sorry - you must be running\rat least system 6.0.5 or later");
  454.         
  455.     if (! env.hasColorQD)
  456.         MemoryError("Sorry - you must have a\rMacintosh with Color QuickDraw" );
  457.         
  458.     /*
  459.      * If the QD32 trap (AB04) is really the unimplemented trap (A89F),
  460.      * then QD32 isn't around.  This is from Bruce's "develop" article.
  461.      */
  462.     if (NGetTrapAddress( 0xAB03, ToolTrap ) == NGetTrapAddress( 0xA89F, ToolTrap ))
  463.         MemoryError("Sorry - 32 bit Color Quickdraw is not present");
  464. }
  465.  
  466. /*
  467.  * Startup / initialization
  468.  */
  469.  
  470. void InitMac()
  471. {
  472.     MaxApplZone();
  473.     FlushEvents( everyEvent - diskMask, 0 );
  474.     InitGraf(&thePort);
  475.     InitFonts();
  476.     InitWindows();
  477.     InitMenus();
  478.     TEInit();
  479.     InitDialogs( NIL );
  480.     InitCursor();
  481.     CheckGoodMac();
  482. }
  483.